package com.longrise.android.loaddata;

import android.util.Log;

import java.io.File;
import java.util.Map;

public class AnsynHttpRequest 
{
	public static final int REQUEST_TIMEOUT = 10*1000;//设置请求超时10秒钟
	public static final int SO_TIMEOUT = 10*1000;  //设置等待数据超时时间10秒钟
	static final int POST = 1; // post 提交
	static final int GET = 2; // get 提交
    private static final String TAG = "AnsynHttpRequest";
	
    //下载器列表
    //private static List<LoadFileRunnable> taskList = new ArrayList<LoadFileRunnable>();
    
	/**
	 * get请求
	 * @param callBack
	 * @param url
	 * @param serviceName
	 * @param map
	 * @param isCache
	 */
	public static void requestByGet(String appVersion,String url,String serviceName,Map<String, String> map,
			boolean isCache,ObserverCallBack callBack)
	{
		if(url == null || null== serviceName)
		{
			if(null != callBack)
			{
				callBack.back(null, url,null,null);
			}
			return;
		}
		
		//拼接请求地址
		StringBuffer stringBuffer = new StringBuffer(url);
		stringBuffer.append("/"+serviceName+"/query");
		
		if(map != null)
		{
			int i = 0;
			for (String key : map.keySet()) 
			{
				if(i == 0)
				{
					stringBuffer.append("?");
					stringBuffer.append(key + "=" + map.get(key));
				}
				else if(map.keySet().size()-1 == i)
				{
					stringBuffer.append(key + "=" + map.get(key));
				}
				else
				{
					stringBuffer.append(key + "=" + map.get(key) + "&");
				}
				i++;
			}
		}
		
		// 异步请求数据
		doAsynRequest(appVersion,GET, callBack, url, map,isCache);
	}
	
	/**
	 * post请求
	 * @param callBack
	 * @param url
	 * @param serviceName
	 * @param map
	 * @param isCache
	 */
	public static void requestByPost(String appVersion,String url,String serviceName,Map<String, String> map,
			boolean isCache,ObserverCallBack callBack)
	{
		// 异步请求数据
		doAsynRequest(appVersion,POST, callBack, url+"/"+serviceName+"/query", map,isCache);
	}

	/**
	 * 开始请求
	 * @param sendType
	 * @param callBack
	 * @param url
	 * @param url
	 * @param map
	 * @param isCache
	 */
	private static void doAsynRequest(String appVersion,int sendType, ObserverCallBack callBack,
			String url, Map<String, String> map,
			boolean isCache) 
	{
		// 请求
		ThreadPoolUtils.execute(new MyRunnable(appVersion,sendType, callBack, url, map, isCache));
	}
	
	/**
	 * 下载文件
	 * @param url
	 * 			下载完整地址
	 * @param localpath
	 * 			存储的本地完整路径
	 * @param onDownLoadFileListener
	 * 			监听接口
	 */
	public static LoadFileRunnable loadFileRequest(String url,String localpath,OnDownLoadFileListener onDownLoadFileListener)
	{
	    LoadFileRunnable loadFileRunnable = new LoadFileRunnable(url, localpath, onDownLoadFileListener);
		// 请求
		ThreadPoolUtils.execute(loadFileRunnable);
		LoadDataManager.getInstance().getTaskList().add(loadFileRunnable);

		return loadFileRunnable;
	}
	
	/**
     * 取消某个文件下载任务任务
     * @param url
     */
    public static void cancelUpLoadFileTaskByUrl(String url , String localpath)
    {
        if(url == null || "".equals(url))
        {
            return;
        }
        
         Log.d(TAG, "url1:"+url);
        if(null != LoadDataManager.getInstance().getTaskList())
        {
            Log.d(TAG, "taskList:"+ LoadDataManager.getInstance().getTaskList().size());
            for (int i = 0; i < LoadDataManager.getInstance().getTaskList().size(); i++)
            {
                Log.d(TAG, "i:"+i);
                LoadFileRunnable loadFileRunnable = LoadDataManager.getInstance().getTaskList().get(i);
                if(null != loadFileRunnable)
                {
                    Log.d(TAG, "loadFileRunnable:"+loadFileRunnable.getUrl());
                                         
                    loadFileRunnable.setIscopy(false);
                    
                    if(url.equals(loadFileRunnable.getUrl()))
                    {
                        Log.d(TAG, "url:"+url);
                        //从线程池中移除
                        ThreadPoolUtils.remove(loadFileRunnable);
                        
                        //删除缓存
                        String tempFilePathString = loadFileRunnable.getLocalpath();
                        if(null != tempFilePathString)
                        {
                            Log.d(TAG, "tempFilePathString:"+tempFilePathString);
                            File file = new File(tempFilePathString);
                            if(file.exists())
                            {
                                file.delete();
                            }
                        }
                        
                        if(null!=localpath){
                            Log.d(TAG, "localpath:"+tempFilePathString);
                            File file = new File(tempFilePathString);
                            if(file.exists())
                            {
                                file.delete();
                            }
                        }
                        
                        break;
                    }
                }
            }
        }
    }
    
    
    /**
     * 暂停某个文件下载任务任务
     * @param url
     */
    public static void stopUpLoadFileTaskByUrl(String url , String localpath)
    {
        if(url == null || "".equals(url))
        {
            return;
        }
        
         Log.d(TAG, "url1:"+url);
        if(null != LoadDataManager.getInstance().getTaskList())
        {
            Log.d(TAG, "taskList:"+ LoadDataManager.getInstance().getTaskList().size());
            for (int i = 0; i < LoadDataManager.getInstance().getTaskList().size(); i++)
            {
                Log.d(TAG, "i:"+i);
                LoadFileRunnable loadFileRunnable = LoadDataManager.getInstance().getTaskList().get(i);
                if(null != loadFileRunnable)
                {
                    Log.d(TAG, "loadFileRunnable:"+loadFileRunnable.getUrl());
                                         
                    loadFileRunnable.setIscopy(false);
                    
                    if(url.equals(loadFileRunnable.getUrl()))
                    {
                        Log.d(TAG, "url:"+url);
                        //从线程池中移除
                        ThreadPoolUtils.remove(loadFileRunnable);
                        
//                        //删除缓存
//                        String tempFilePathString = loadFileRunnable.getTempFilePath();
//                        if(null != tempFilePathString)
//                        {
//                            Log.d(TAG, "tempFilePathString:"+tempFilePathString);
//                            File file = new File(tempFilePathString);
//                            if(file.exists())
//                            {
//                                file.delete();
//                            }
//                        }
//                        
//                        if(null!=localpath){
//                            Log.d(TAG, "localpath:"+tempFilePathString);
//                            File file = new File(tempFilePathString);
//                            if(file.exists())
//                            {
//                                file.delete();
//                            }
//                        }
                     
                    }
                }
            }
        }
    }
    
	
}
